#include "SudokuChromosomeFactory.h"
#include "SudokuChromosome.h"
#include "parameters.h"

SudokuChromosomeFactory::SudokuChromosomeFactory(SudokuData sudokuData) : AbstractChromosomeFactory(0)
{
    TRACE_FUNC

    sudokuDiagramSize = sudokuData.getSize();
    cluesArr = new QList<QList<int> >();

    qDebug() << sudokuData.getCellCount();
    for(int i=0;i<sudokuData.getCellCount();++i)//iterate cells
    {
        //qDebug() << "!";
        if(sudokuData.cellAt(i).getValue() == 0)//if cell is not filled
        {
            chromosomeLength++;
            chromosomeCellIndexList.append(i);
            //qDebug() << sudokuData.cells.at(i);
            QList<int> tmpPossList;
            for(int j=0;j<sudokuData.getSize();++j)
            {

                if(sudokuData.cellAt(i).getPossibl(j) == true)
                    tmpPossList.append(j+1);
            }
            if(tmpPossList.isEmpty())
                qFatal("wrong sudoku");

            QString ttt("");
            for(int k=0;k<tmpPossList.size();++k)
                ttt += QString::number(tmpPossList.at(k)) + ", ";
            qDebug() << ttt;

            cluesArr->append(tmpPossList);
        }
    }

    for(int i=0;i<chromosomeCellIndexList.size();++i)
        qDebug() << chromosomeCellIndexList.at(i);

    qDebug() << "chromosomeLength: " << chromosomeLength;
}

SudokuChromosomeFactory::~SudokuChromosomeFactory()
{
    delete cluesArr;
}

Chromosome* SudokuChromosomeFactory::getNewChromosome()
{
    TRACE_FUNC
    return getNewChromosome(chromosomeLength);//new SudokuChromosome(chromosomeLength, cluesArr, chromosomeCellIndexList);
}

Chromosome* SudokuChromosomeFactory::getNewChromosome(int len)
{
    TRACE_FUNC
    return new SudokuChromosome(len, cluesArr, chromosomeCellIndexList, sudokuDiagramSize);
}
